package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult pageQuery(StudentQueryParam studentQueryParam) {
        // 设置分页参数
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        // 执行查询
        List<Student> studentList = studentMapper.pageQuery(studentQueryParam);
        Page<Student> pageInfo = (Page<Student>) studentList;
        // 封装返回结果
        return new PageResult(pageInfo.getTotal(), pageInfo.getResult());
    }

    @Override
    @Transactional
    public void deleteByIds(List<Integer> id) {
        studentMapper.deleteByIds(id);
    }

    @Override
    public void violation(Integer id, Integer score) {
        studentMapper.updateViolation(id,score);
    }


    @Override
    @Transactional
    public void add(Student student) {
        LocalDateTime now = LocalDateTime.now();
        student.setCreateTime(now);
        student.setUpdateTime(now);
        studentMapper.insert(student);
    }

    @Override
    public Student getById(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    @Transactional
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

}


